package com.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class TokenUtil {

    private final static Logger log = LoggerFactory.getLogger(TokenUtil.class);

    static String magicNumber = "1841";

    public static String digestSHA256(String src, String salt) {
        try {
            MessageDigest sha256 = MessageDigest.getInstance("sha-256", "SUN");
            sha256.update(src.getBytes());

            if (salt != null) {
                sha256.update(salt.getBytes());
            }
            byte[] result = sha256.digest();

            return new String(Hex.encodeHex(result));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String getToken(String userId, String requestTime) {
        String src = String.format("%s%s%s", userId, requestTime, magicNumber);
        String token = digestSHA256(src, null);
        return token;
    }

}
